from django.core.management.base import NoArgsCommand

from stream import models, feeds
from stream.testcases import feedprovider, utils
from stream.selenium import selenium

from datetime import datetime
import subprocess
import os
import signal

class Command(NoArgsCommand):
    def handle_noargs(self, **options):
        from django.core.management import call_command
        call_command('flush', interactive = False)
        # Some mocking will take place
        feed_provider = feedprovider.FeedProvider()
        feeds.retrieve_feed.mock(feed_provider.retrieve_feed)
        feeds.find_feed.mock(feed_provider.find_feed)
        now_time = datetime(2009, 9, 10, 0, 0, 0) # So the feeds be recognized as stale.
        models.now.mockValue(now_time)

        sel_server_pid = os.spawnlp(os.P_NOWAIT, "java", "java", "-jar", "./stream/selenium-server.jar", "-firefoxProfileTemplate", "/home/commando/.mozilla/firefox/myx1fzcb.development")

        sel = selenium("localhost", 4444, "*chrome", "http://127.0.0.1:8000/")
        sel.start()

        # ---- First user ----
        sel.open("/")
        sel.click("link=Login")
        sel.wait_for_page_to_load("30000")
        sel.click("css=a.google")
        sel.wait_for_page_to_load("30000")
        sel.click("openid_message")
        sel.wait_for_page_to_load("30000")

        sel.click("link=Go to your content")
        sel.wait_for_page_to_load("30000")

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://google.com")
        sel.type("id_tags", "google search")
        sel.click("id_published")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://yandex.ru")
        sel.type("id_tags", "yandex search")
        sel.click("id_published")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        models.FeedChannel.subscribe(url = feed_provider.russian_feed_link, user = models.User.objects.all()[0])

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://cplusplus.com")
        sel.type("id_tags", "c++ site")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Logout")
        sel.wait_for_page_to_load("30000")

        # ---- Second user ----
        sel.open("/")
        sel.click("link=Login")
        sel.wait_for_page_to_load("30000")
        sel.click("css=a.yandex")
        sel.type("openid_username", "d-mon")
        sel.click("openid_submit")
        sel.wait_for_page_to_load("30000")
        sel.click("openid_message")
        sel.wait_for_page_to_load("30000")

        sel.click("link=Go to your content")
        sel.wait_for_page_to_load("30000")
        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://yandex.ru")
        sel.type("id_tags", "search mainpage")
        sel.click("id_published")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://rambler.ru")
        sel.type("id_tags", "rambler mainpage")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://cpluscplus.com")
        sel.type("id_tags", "c++ site")
        sel.click("id_published")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        sel.click("//button[@type='button']")
        sel.wait_for_page_to_load("30000")
        sel.type("id_link", "http://fhtagn.ru")
        sel.type("id_tags", "fhtagn")
        sel.click("id_published")
        sel.click("//input[@value='Add']")
        sel.wait_for_page_to_load("30000")

        models.FeedChannel.subscribe(url = feed_provider.codinghorror_feed_link, user = models.User.objects.all()[1])

        sel.stop()
        os.kill(sel_server_pid, signal.SIGTERM)
